<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10">
        <div class="mainor_form">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="90px"
             id="form"
          >
            <el-form-item label="入库状态：" prop="status">
              <el-select
                v-model.trim="form.status"
                placeholder="入库状态"
                class="wid220"
                clearable
                @change="getArticleList"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(statusList, index) in list.statusList"
                  :key="index"
                  :label="statusList"
                  :value="parseInt(index + 1)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="入库类型：" prop="type">
              <el-select
                v-model.trim="form.type"
                placeholder="入库类型"
                class="wid220"
                clearable
                @change="getArticleList"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(typeList, index) in list.typeList"
                  :key="index"
                  :label="typeList"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="入库时间：" prop="createTime">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
                @change="getArticleList"
              />
            </el-form-item>
            <MainFormBtn @reset="reset" @getSearchFor="getSearchFor" :loading="listLoading" :showBtn="moredata">
              <el-button @click="exportStore">导出</el-button>
            </MainFormBtn>
            <div class="more el-form-item" @click="more">
              <span>更多</span>
              <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
            </div>
            <div class="clearfix"></div>
            <el-collapse-transition>
              <div v-show="moredata">
                <el-form-item label="入库单号：" prop="order_sn">
                  <el-input
                    v-model.trim="form.order_sn"
                    placeholder="入库单号"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item>
                <el-form-item label="关联单号：" prop="related_order_sn">
                  <el-input
                    v-model.trim="form.related_order_sn"
                    placeholder="关联单号"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item>
                <el-form-item label="采购方式：" prop="mode">
                  <el-select
                    v-model.trim="form.mode"
                    placeholder="采购方式"
                    class="wid220"
                    clearable
                    @change="getArticleList"
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(modeList, index) in list.modeList"
                      :key="index"
                      :label="modeList"
                      :value="parseInt(index)"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="采购员：" prop="buyer_id">
                  <el-select
                    v-model.trim="form.buyer_id"
                    placeholder="采购员"
                    class="wid220"
                    clearable
                    @change="getArticleList"
                    filterable
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(buyer, index) in list.buyer"
                      :key="index"
                      :label="buyer"
                      :value="parseInt(index)"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <div class="clearfix"></div>
                <el-form-item label="供应商：" prop="supplier_id">
                  <el-select
                    v-model.trim="form.supplier_id"
                    placeholder="供应商"
                    class="wid220"
                    clearable
                    @change="getArticleList"
                    filterable
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(supplier, index) in list.supplier"
                      :key="index"
                      :label="supplier"
                      :value="parseInt(index)"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="创建日期：" prop="establishTime">
                  <el-date-picker
                    v-model="establishTime"
                    type="daterange"
                    class="time220"
                    :editable="false"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                    :clearable="false"
                    :picker-options="pickerOptions"
                    @change="getArticleList"
                  />
                </el-form-item>
                <div class="clearfix"></div>
                <el-form-item label=" ">
                  <MainFormBtn class="btn-bottom" @reset="reset" @getSearchFor="getSearchFor" :loading="listLoading" :showBtn="!moredata">
                    <el-button @click="exportStore">导出</el-button>
                  </MainFormBtn>
                </el-form-item>
              </div>
            </el-collapse-transition>
          </el-form>
        </div>
        <div class="main-controls-wrap">
          <div>
            <router-link to="/warehouse/receipt/add" class="mainor_tea">创建入库单</router-link>
            <el-button @click="batchPrint" class="mg-l-10">批量打印</el-button>
          </div>
        </div>
        <div class="main_table">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
            :header-cell-class-name="this.$verticaline"
            :height="borderHeight"
            :header-cell-style="tableHeightStyle"
            @header-dragend="changeTableWidth"
            @row-dblclick="rowDblclick"
          >
            <el-table-column type="selection" />
            <el-table-column
              label="入库单号"
              prop="order_sn"
              :width="tableWidth[hash + 'order_sn']"
            />

            <el-table-column
              label="入库类型"
              prop="type"
              :width="tableWidth[hash + 'type']"
            >
              <template slot-scope="scope">{{
                list.typeList[scope.row.type]
              }}</template>
            </el-table-column>
            <el-table-column
              label="关联单号"
              prop="related_order_sn"
              :width="tableWidth[hash + 'related_order_sn']"
            />
            <el-table-column
              label="入库金额"
              prop="money"
              :width="tableWidth[hash + 'money']"
            />
            <el-table-column
              label="入库状态"
              prop="status"
              :width="tableWidth[hash + 'status']"
            >
              <template slot-scope="scope"
                ><span :class="'order' + scope.row.status">
                  {{ list.statusList[scope.row.status] }}</span
                ></template
              >
            </el-table-column>
            <el-table-column
              label="创建时间"
              prop="created_at"
              :width="tableWidth[hash + 'created_at']"
            >
              <template slot-scope="scope">{{
                parseTime(scope.row.created_at, "{y}-{m}-{d}")
              }}</template>
            </el-table-column>
            <el-table-column
              label="入库时间"
              prop="updated_at"
              :width="tableWidth[hash + 'updated_at']"
            >
              <template slot-scope="scope">{{
                scope.row.storage_time
                  ? parseTime(scope.row.storage_time, "{y}-{m}-{d}")
                  : ""
              }}</template>
            </el-table-column>
            <el-table-column
              label="管理操作"
              align="left"
              fixed="right"
              prop="operate"
              :width="tableWidth[hash + 'operate'] || 240"
              class-name="main_hei"
              label-class-name="main-center"
            >
              <template slot-scope="scope">
                <router-link :to="'/warehouse/receipt/see/' + scope.row.id">
                  <el-link type="success">查看</el-link>
                </router-link>
                <router-link :to="'/warehouse/receipt/edit/' + scope.row.id">
                  <el-link type="warning" v-if="scope.row.status == 0 || scope.row.status == 3"
                    >审核</el-link
                  >
                </router-link>
                <el-link
                  style="color: rgb(17, 138, 178)"
                  @click="exportOrderiInfo(scope.row.id)"
                  >导出</el-link
                >
                <el-link
                  @click="print(scope.row.id)"
                  v-if="scope.row.order_status != 0"
                  >打印</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <MainPagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- 入库单导出 -->
    <ExportDialog :visible.sync="exportVisible" :statusList="list.statusList" :typeList="list.typeList"></ExportDialog>
    <!-- 打印子组件 -->
    <order-print
      ref="printTemplate"
      v-show="false"
      id="printname"
    ></order-print>
  </div>
</template>

<script>
import { mixins } from "@/mixins/index.js";
import {
  storage_index,
  export_storage_info,
} from "@/api/warehouse/warehouseReceipt";
import { apiUrl, objKeySort, Md5, packApi } from "@/utils/public";
import dateOption from "@/options/dateOption";
import OrderPrint from "@/views/order/orderPrint.vue";
import { parseTime, exportMethod } from "@/utils/index";
import ExportDialog from "@/views/warehouse/components/receipt/exportDialog.vue";
export default {
  mixins: [mixins],
  components: {
    OrderPrint,
    ExportDialog,
  },
  data() {
    return {
      // 时间
      parseTime,
      exportMethod,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: [],
      },
      listLoading: false,
      // 搜索
      form: {
        status: null,
        type: null,
        order_sn: "",
        related_order_sn: "",
        storage_start_time: "",
        storage_end_time: "",
        start_time: "",
        end_time: "",
        mode: null,
        buyer_id: null,
        supplier_id: null,
      },
      createTime: [],
      establishTime: [],
      pickerOptions: dateOption,
      // 更多
      moredata: false,
      // 全选
      selectArr: [],
      exportVisible: false, // 入库单导出弹窗状态
    };
  },
  activated() {
    if (this.$route.params.status) this.form.status = parseInt(this.$route.params.status);
    // 列表
    if (this.$store.state.app.storagekeep) {
      this.storage_index();
      this.$store.commit("app/STORAGECHANGE", false);
    } else {
      this.form = {
        status: null,
        type: null,
        order_sn: "",
        related_order_sn: "",
        storage_start_time: "",
        storage_end_time: "",
        start_time: "",
        end_time: "",
        mode: null,
        buyer_id: null,
        supplier_id: null,
      };
      this.createTime = [];
      this.establishTime = [];
      this.current = {
        page: 1,
        per_page: 20,
      };
      if (this.$route.params.status) this.form.status = parseInt(this.$route.params.status);
      this.moredata = false;
      this.limit = 20;
      this.totalCount = 1;
      this.storage_index();
    }
  },
  created() {
    this.getNumerical(210);
  },
  methods: {
    //  表格双击跳转详情页面
    rowDblclick(row, column, event) {
      this.$router.push('/warehouse/receipt/see/' + row.id)
    },
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.establishTime = [];
      this.createTime = [];
      this.storage_index();
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push({ id: item.id });
      });
    },
    // 批量打印
    batchPrint() {
      let length = this.selectArr.length;
      if(!length) return this.$message.error("请勾选订单");
      this.$confirm(
        `此操作将连续打印【${length}】张入库单,并且打印机需要设置为后进纸,是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(async () => {
        for (let index = 0; index < length; index++) {
          const item = this.selectArr[index];
          await this.print(item.id, "continuous");
        }
      })
    },
    // 打印
    print(id, continuous=null) {
      return new Promise((resolve,reject)=> {
        this.$refs.printTemplate.$emit("demo", {
          id: id,
          continuous: continuous,
          type: "storage",
          cd: (res) => {
            resolve();
          },
        });
      })
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.storage_index();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.storage_index();
    },
    changePage(page) {
      this.current.page = page;
      this.storage_index();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 列表
    storage_index() {
      this.listLoading = true;
      let data = packApi({
        ...this.list_form(),
        ...this.current
      });
      storage_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";
      form.storage_start_time = this.establishTime[0]
        ? this.establishTime[0]
        : "";
      form.storage_end_time = this.establishTime[1]
        ? this.establishTime[1]
        : "";

      return form;
    },
    // 入库单导出
    exportStore() {
      this.exportVisible = true;
    },
    exportOrderiInfo(id) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id }
      );
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      export_storage_info(data)
        .then((res) => {
          this.exportMethod(res, "入库管理单", notify);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
    // 点击搜索时将多余的搜索条件折叠起来
    getSearchFor() {
      this.getArticleList();
    },
    startGetting() {
      this.getSearchFor();
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: 100%;
  }
}

// 支付状态
.success {
  color: $green;
}
.unpaid {
  color: $colour_red;
}
// 订单状态
.order0 {
  color: $order_color0;
}
.order1 {
  color: $order_color3;
}
.order2 {
  color: $order_color4;
}
.order3 {
  color: $order_color1;
}

/deep/.el-form-item--small.el-form-item {
  margin-bottom: 10px;
}
</style>
